<template>
    <!-- 调拨单页面 -->
    <div class="app-container transfer-page">
        <el-form ref="form" :model="postData" label-width="80px">
            <el-row :gutter="10" class="sumbit-btn-group mb8">
                <el-form-item label="单据日期">
                    <el-date-picker
                        v-model="postData.receiptsTime"
                        type="date"
                        placeholder="选择日期"
                    />
                </el-form-item>
                <el-col class="flex-center" :span="1.5">
                    <span class="sub-text"
                        >单据编号: {{ postData.transferId }}</span
                    >
                </el-col>
                <div class="empty"></div>

                <el-col :span="1.5" v-if="id">
                    <el-button
                        type="primary"
                        plain
                        size="mini"
                        @click="clearParams"
                        v-hasPermi="['storage:transfer_record:add']"
                        >新增</el-button
                    >
                </el-col>
                <el-col :span="1.5" v-if="!id">
                    <el-button
                        type="primary"
                        plain
                        size="mini"
                        v-hasPermi="['storage:transfer_record:add']"
                        @click="savePurchase(1)"
                        >保存并新增</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        size="mini"
                        v-hasPermi="['storage:transfer_record:add']"
                        @click="savePurchase(0)"
                        >保存</el-button
                    >
                </el-col>

                <el-col :span="1.5" v-if="id">
                    <el-button
                        type="success"
                        plain
                        size="mini"
                        @click="handleAudit()"
                        v-hasPermi="['storage:woodTransferRecord:editAuditState']"
                        >{{
                            postData.auditStatus == 1 ? "反审核" : "审核"
                        }}</el-button
                    >
                </el-col>

                <el-col :span="1.5">
                    <el-button
                        plain
                        size="mini"
                        @click="
                            $router.push({
                                path: '/depot/receipt/transfer/list',
                            })
                        "
                        v-hasPermi="['storage:woodTransferRecord:list']"
                        >历史单据</el-button
                    >
                </el-col>
            </el-row>

            <JustTimeInventory @selectItemsClick="selectItemsClick" />

            <div class="table-area">
                <TableForm
                    ref="TableForm"
                    :column="tableFormColum"
                    :list="postData.woodTransferRecordGoodsList"
                    :loading="loading"
                />
                <el-form-item class="form-remark" prop="remark">
                    <el-input
                        v-model="postData.remark"
                        type="textarea"
                        placeholder="请输入备注信息"
                        clearable
                        size="small"
                        rows="4"
                        :disabled="disabled"
                    />
                </el-form-item>
            </div>
        </el-form>
    </div>
</template>

<script>
import TableForm from "@/components/TableForm/preset";
import JustTimeInventory from "@/components/JustTimeInventory";

export default {
    components: {
        TableForm,
        JustTimeInventory,
    },
    data: () => {
        return {
            id: "",
            postData: {
                transferId: "",
                receiptsTime: Date.now(),
                auditStatus: 0,
                woodTransferRecordGoodsList: [
                    {
                        allotInCount: 0,
                        baleNum: "",
                        brandName: "",
                        createBy: "",
                        createTime: "",
                        delFlag: "",
                        gcountId: "",
                        goodsName: "",
                        inGoodsCountId: "",
                        inWarehouse: "",
                        itemId: "",
                        length: 0,
                        level: "",
                        outWarehouse: "",
                        params: {},
                        pieceCount: 0,
                        ply: 0,
                        remark: "",
                        searchValue: "",
                        sliceCount: 0,
                        storage: "",
                        transferId: "",
                        updateBy: "",
                        updateTime: "",
                        width: 0,
                    },
                ],
                remark: "",
            },
            disabled: false,
            loading: false,
            tableFormColum: [
                { label: "包号" },
                { label: "品名", inputType: "none" },
                { label: "品牌", inputType: "none" },
                { label: "等级", inputType: "none" },
                { label: "厚度", inputType: "none" },
                { label: "宽度", inputType: "none" },
                { label: "长度", inputType: "none" },
                { label: "片数", inputType: "none" },
                { label: "件数" },
                { label: "调拨方数", inputType: "none" },
                { label: "调出仓库", inputType: "none", value: "outWarehouse" },
                { label: "调入仓库", value: "inWarehouse" },
                { label: "库位(批量)" },
                { label: "备注" },
            ],
        };
    },
    created() {
        let { id } = this.$route.params;
        // 创建单据编号
        id ? this.getTransferDetail(id) : this.creatreNo();
    },
    methods: {
        // 获取详情
        getTransferDetail(id) {
            this.id = id;
            this.$Api.storage.transfer.detail(id).then((res) => {
                let d = res.data || {
                    woodTransferRecordGoodsList: [],
                };
                d.woodTransferRecordGoodsList.forEach((v) => {
                    v.inCount = v.allotInCount;
                });

                this.postData = d;
            });
        },
        // 创建单据编号
        creatreNo() {
            this.$Api.storage.transfer.creatreNo().then((res) => {
                this.postData.transferId = res.data || "";
            });
        },
        // 包 数据选择
        selectItemsClick(data) {
            let d = JSON.parse(JSON.stringify(data));
            this.postData.woodTransferRecordGoodsList = d;
        },
        savePurchase(clear = false) {
            let { postData, loading, id } = this;

            if (loading) return;

            this.$refs["TableForm"].checkForm((pass, error) => {
                let { add, update } = this.$Api.storage.transfer;
                let api = id ? update : add;

                console.log(update)

                this.loading = true;

                postData.woodTransferRecordGoodsList.forEach((v) => {
                    v.allotInCount = v.inCount;
                });

                api(postData)
                    .then((res) => {
                        let { msg, code } = res;
                        !id && (this.id = res.data);
                        this.$message.success(msg);
                        this.loading = false;
                        code == 200 && clear && this.clearParams();
                    })
                    .catch(() => {
                        this.loading = false;
                    });
            });
        },
        handleAudit() {
            let { id, loading } = this;

            if (loading) return;
            this.loading = true;

            let { auditStatus } = this.postData;
            let _auditStatus = auditStatus==1 ? 0 : 1;
            this.$Api.storage.transfer
                .audit({
                    transferIdArray: [id],
                    auditStatus: _auditStatus,
                })
                .then((res) => {
                    this.$message.success("操作成功");
                    this.postData.auditStatus = _auditStatus;
                    setTimeout(() => {
                        this.loading = false;
                    }, 300);
                })
                .catch(() => {
                    setTimeout(() => {
                        this.loading = false;
                    }, 300);
                });
        },
        clearParams() {
            this.id = "";
            this.postData = {
                transferId: "",
                receiptsTime: Date.now(),
                auditStatus: 0,
                woodTransferRecordGoodsList: [
                    {
                        allotInCount: 0,
                        baleNum: "",
                        brandName: "",
                        createBy: "",
                        createTime: "",
                        delFlag: "",
                        gcountId: "",
                        goodsName: "",
                        inGoodsCountId: "",
                        inWarehouse: "",
                        itemId: "",
                        length: 0,
                        level: "",
                        outWarehouse: "",
                        params: {},
                        pieceCount: 0,
                        ply: 0,
                        remark: "",
                        searchValue: "",
                        sliceCount: 0,
                        storage: "",
                        transferId: "",
                        updateBy: "",
                        updateTime: "",
                        width: 0,
                    },
                ],
            };
            this.creatreNo();
        },
    },
};
</script>

<style lang="scss" scoped>
::v-deep.transfer-page {
    .sumbit-btn-group {
        position: relative;
        z-index: 100;
        margin: 10px 0 20px 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .empty {
            width: 100%;
            flex: 1;
        }
        .sub-text {
            margin-left: 20px;
            font-size: 12px;
            color: #262626;
            user-select: none;
        }
    }

    .el-form-item {
        margin: 0;
    }
    .form-remark {
        display: block;
        display: block;
        margin: 20px 15px 15px;
        .el-form-item__content {
            margin: 0 !important;
            width: 100%;
        }
    }
}
</style>

